# -*- coding: utf-8 -*-

# @Time : 2022/6/14 10:55

# @Author : scy

# @File : __init__.py.py

# @Software: PyCharm

# 工作人员
from interact import interactSql


class worker:
    # 初始化
    def __init__(self, ):

        self.__username = None
        self.__password = None

    # 登录
    def login(self, waccount, wpassword):

        self.__account = waccount
        self.__password = wpassword

        # 从数据库中取出数据查询是否能登录
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            # 取出数据
            conn.run_sql('select * from staff where account = "%s"' % self.__account)
            staff_message = conn.get_results()
            if len(staff_message) == 0:
                return '工作人员不存在不存在'
            if staff_message[0][2] == self.__password:
                # 赋值
                (self.__staffname, self.__account, self.__password) = staff_message[0]
                return '登陆成功'
            else:
                return '密码错误，请重新输入'
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 查询管理的用户
    def inquiry_user(self):
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from user where staff = "%s"' % self.__account)
            all_user = conn.get_results()
            if len(all_user) == 0:
                return '工作人员还未关联用户'
            return all_user
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 管理的相亲地点
    def inquiry_manage_place(self):
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from place where staff = "%s"' % self.__account)
            all_place = conn.get_results()
            if len(all_place) == 0:
                return '工作人员还未管理地点'
            return all_place
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 查询所有地点
    @staticmethod
    def inquiry_place():
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('select * from place')
            return conn.get_results()
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 修改用户状态
    def modify_user_state(self, phone_number, state):
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            conn.run_sql('update user set own_state = "%s" where phone_number = "%s"' % (state, phone_number))
            conn.commit()
            return '修改成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')
